// src/main/java/com/zy/product/controller/CartController.java
package com.zy.orders.web.controllers;

import com.zy.orders.service.CartService;
import com.zy.web.bean.CartDTO;
import com.zy.web.bean.ResponseResult;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
/**
 * 购物车控制器
 */
@RestController
@RequestMapping("/orders/cart")
@AllArgsConstructor
public class CartController {

    private final CartService cartService;

    // TODO: 如果需要，可以注入其他服务，例如 UserService 来获取更详细的用户信息

    /**
     * 添加商品到购物车或更新商品数量
     * POST /orders/cart
     */
    @PostMapping
    public ResponseEntity<ResponseResult> addItem(
            @RequestHeader("X-User-ID") Long userId, // 从请求头中获取用户ID
            @RequestBody CartAddItemRequest request) {
        try {
             cartService.addItemToCart(userId, request.getProductInfo(), request.getQuantity());
            //查询出此用户的购物车列表
            List<CartDTO> cartItems = cartService.getCartItems(userId);
            return new ResponseEntity<>(ResponseResult.ok("商品成功添加到购物车").setData(cartItems), HttpStatus.CREATED);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(ResponseResult.error(e.getMessage()), HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(ResponseResult.error("添加购物车失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 更新购物车中商品的数量
     * PUT /orders/cart/updateQuantity
     */
    @PutMapping("/updateQuantity")
    public ResponseEntity<ResponseResult> updateQuantity(
            @RequestHeader("X-User-ID") Long userId, // 从请求头中获取用户ID
            @RequestBody CartDTO cartDTO) {
        CartDTO result = cartService.updateCartItemQuantity(userId, cartDTO.getCartId(), cartDTO.getQuantity());
        if (result != null) {
            return new ResponseEntity<>(ResponseResult.ok( "购物车商品数量更新成功").setData(result), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(ResponseResult.error("购物车商品数量更新失败，可能商品不存在或不属于该用户"), HttpStatus.NOT_FOUND);
        }
    }

    /**
     * 从购物车中删除指定商品
     * DELETE /orders/cart/deleteItem/{cartId}
     */
    @DeleteMapping("/deleteItem/{cartId}")
    public ResponseEntity<ResponseResult> deleteItem(
            @RequestHeader("X-User-ID") Long userId, // 从请求头中获取用户ID
            @PathVariable("cartId") Long cartId) {
        boolean success = cartService.deleteCartItem(userId, cartId);
        if (success) {
            return new ResponseEntity<>(ResponseResult.ok("购物车商品删除成功").setData(true), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(ResponseResult.error("购物车商品删除失败，可能商品不存在或不属于该用户"), HttpStatus.NOT_FOUND);
        }
    }

    /**
     * 获取用户购物车中的所有商品列表
     * GET /orders/cart/items
     */
    @GetMapping("/items")
    public ResponseEntity<ResponseResult> getCartItems(
            @RequestHeader("X-User-ID") Long userId) { // 从请求头中获取用户ID
        List<CartDTO> items = cartService.getCartItems(userId);
        if(  items.size()<=0){
            items=new ArrayList<>();
        }
        return new ResponseEntity<>(ResponseResult.ok( "获取购物车列表成功").setData( items), HttpStatus.OK);
    }

    /**
     * 清空用户购物车
     * DELETE /orders/cart/clear
     */
    @DeleteMapping("/clear")
    public ResponseEntity<ResponseResult> clearCart(
            @RequestHeader("X-User-ID") Long userId) { // 从请求头中获取用户ID
        boolean success = cartService.clearCart(userId);
        if (success) {
            return new ResponseEntity<>(ResponseResult.ok("购物车已清空").setData(true), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(ResponseResult.error( "购物车已为空，无需清空").setData(false), HttpStatus.OK);
        }
    }
}
